package com.example.myapplication.fragment;

import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.food_message.Menu;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class VpFragment extends Fragment {
    private GridView gv;
    //当前fragment的View
    private View view;
    //构造传过来的数据
    private List<Menu> foods;
    //需要展示的数据
    private List<Menu> showFoods;
    //gridview适配器
    private GdAdapter gdAdapter = new GdAdapter();
    //上一个 下一个 当前为某一页
    private TextView pre, nex, tag, tagall;
    //当前页
    private int index = 0;
    //展示数据的数量
    private final int count = 10;

    //已经私有 注意
    private VpFragment() {
        // Required empty public constructor
    }

    //新构造
    public VpFragment(List<Menu> foods) {
        this.foods = foods;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_food_reseve, container, false);
        //上一页下一页
        gv = (GridView) view.findViewById(R.id.gride_view);
        pre = view.findViewById(R.id.tv_last_page);
        nex = view.findViewById(R.id.tv_next_page);
        tag = view.findViewById(R.id.tv_page);
        tagall = view.findViewById(R.id.tv_all_page);
        setShowFoodsData();
        //上一页
        pre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (index <= 0) return;
                index--;
                setShowFoodsData();
            }
        });
        //下一页
        nex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (index * count + count >= foods.size()) {
                    return;
                }
                index++;
                setShowFoodsData();
            }
        });
        //gridview适配器
        gv.setAdapter(gdAdapter);

        return view;

    }

    private void setShowFoodsData() {
        //若 一页的页数量乘以当前页数 再加一页页数（即将要展示的数据的最后一条） 大于全部数据的数量
        if (count * index + count > foods.size()) {
            //只展示到 一页的页数量乘以当前页数（即第将要展示的数据的第一条） 到 全部数量的最后一条
            //showFoods = foods.subList(count * index, foods.size()-1);
            showFoods = foods.subList(count * index, foods.size());
        } else {
            //否则 全部展示
            showFoods = foods.subList(count * index, count * index + count);
        }
        if (gdAdapter != null) {
            //如果适配器不为空 适配器更新数据源
            gdAdapter.notifyDataSetChanged();
        }
        //设置当前页数
        tag.setText(index + 1 + "");
        //显示总页数
        int all = foods.size() / count;
        if (foods.size() % count != 0) {
            all++;
        }
        tagall.setText(all+"");
    }


    //gridview适配器类 基本适配器
    class GdAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return showFoods.size(); //使用需要展示的数据
        }

        @Override
        public Object getItem(int i) {
            return showFoods.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            //解析view容器
            view = LayoutInflater.from(getActivity()).inflate(R.layout.item_meal_food, viewGroup, false);
            //找到控件
            TextView name = view.findViewById(R.id.item_name);
            TextView oldPrice = view.findViewById(R.id.tv_price);
            TextView newPrice = view.findViewById(R.id.tv_price_normal);

            //设置数据
            Menu food = showFoods.get(i);
            name.setText(food.getmName());
            //折扣价
            oldPrice.setText(food.getmMoneys()+"");
            //原价
            newPrice.setText(food.getmMoney()+"");
            //设置中划线
            newPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            //返回组装好的视图
            return view;
        }
    }
}
